package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Feedback;
import com.example.service.FeedbackService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "反馈模块")
@CrossOrigin
@RequestMapping("/feedback")
@RestController
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @ApiOperation(value = "分页查询反馈")
    @GetMapping("selectFeedback/{current}/{limit}")
    public R selectFeedback(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {

        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");

        Page<Feedback> page = new Page<>(current, limit);
        feedbackService.page(page, wrapper);

        //获取分页后数据
        long total = page.getTotal();
        List<Feedback> list = page.getRecords();

        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "分页带条件查询反馈")
    @PostMapping("selectFeedback/{current}/{limit}")
    public R searchFeedback(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit,
            @RequestBody(required = false) Feedback feedback
    ) {

        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(feedback)) {
            if (!StringUtils.isEmpty(feedback.getUserId())) {
                wrapper.eq("user_id", feedback.getUserId());
            }
        }
        wrapper.orderByDesc("gmt_create");

        Page<Feedback> page = new Page<>(current, limit);
        feedbackService.page(page, wrapper);

        //获取分页后数据
        long total = page.getTotal();
        List<Feedback> list = page.getRecords();

        return R.ok().data("total", total).data("list", list);
    }

}
